﻿@model FCZLM.Core.Models.Products
@using FCZLM.Core.Models
@{
    Layout = null;
    var list =  ViewBag._productCategorysRepository as List<ProductCategorys>;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Exid</title>
    <link href="/layui/css/layui.css" rel="stylesheet" />
</head>
<body>
    <div>
        <div class="layui-card">
            <div class="layui-card-header">编辑产品</div>
            <div class="layui-card-body">
                <form class="layui-form">
                    <input type="hidden" name="Id" value="@Model.Id" />
                    <div class="layui-form-item">
                        <label class="layui-form-label">产品名称</label>
                        <div class="layui-input-block">
                            <input type="text" value="@Model.Name" name="Name" required lay-verify="required" placeholder="请输入要修改的名称" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">产品价格</label>
                        <div class="layui-input-block">
                            <input type="text" value="@Model.Price" name="Price" required lay-verify="required" placeholder="请输入修改金额" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">品牌</label>
                        <div class="layui-input-block">
                            <input type="text" value="@Model.Brand" name="Brand" required lay-verify="required" placeholder="请输入要修改的品牌" autocomplete="off" class="layui-input">
                        </div>
                    </div>

                    <div class="layui-form-item layui-form-text">
                        <label class="layui-form-label">
                            产品描述
                        </label>
                        <div class="layui-input-block">
                            <textarea class="layui-textarea" id="desc" placeholder="请输入内容" style="display: none" name="Desc">@Model.Desc</textarea>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">产品分类</label>
                        <div class="layui-input-block">
                            <select name="ProductCategoryId" lay-verify="required">
                                @foreach (var item in list)
                                {
                                    if (Model.ProductCategoryId == item.Id)
                                    {
                                        <option value="@item.Id" selected>@item.Name</option>
                                    }
                                    else
                                    {
                                        <option value="@item.Id">@item.Name</option>
                                    }
                                }
                            </select>
                        </div>
                    </div>
                     <div class="layui-form-item">
                        <label class="layui-form-label">状态</label>
                        <div class="layui-input-block">
                            @if (Model.Status == 1)
                            {
                                <input type="radio" name="Status" value="1" title="上架" checked>
                                <input type="radio" name="Status" value="0" title="下架" >
                            }
                            else
                            {
                                <input type="radio" name="Status" value="1" title="上架">
                                <input type="radio" name="Status" value="0" title="下架" checked>
                            }

                        </div>
                    </div>
                    <div class="layui-form-item">
                        <div class="layui-input-block">
                            <button class="layui-btn" lay-submit lay-filter="Editform" data-type="text" id="fileNotifySubmit">修改</button>
                            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>
    <script src="/layui/layui.js"></script>
    <script>
        layui.use(['layedit', 'form'], function () {
            var layedit = layui.layedit
                , $ = layui.jquery
                , form = layui.form;
            //设置图片上传
            layedit.set({
                uploadImage: {
                    url: '/fproducturls/index' //图片上传方法的接口url
                    , type: 'post' //默认post
                }
            });
            //构建富文本编辑器，构建方法一定要写在set方法下边
            var index = layedit.build('desc');

            //校验表单的方法，并将富文本编辑器中的内容同步到文本域中
            form.verify({
                content: function (value) {
                    return layedit.sync(index); //同步语句
                }
            });

            //保存表单内容提交的方法
            $('#fileNotifySubmit').on('click', function () {
                var active = {
                    //获取纯文本内容： 111
                    //如果要获取样式，将text替换成 content: <p>111</p>
                    //如果要获取选中的文字，将text替换成selected：111
                    text: function () {
                        //获取富文本编辑器值的语句并赋值到文本域中
                        document.getElementById("desc").innerText = layedit
                            .getContent(index);
                    }
                };

                var type = $(this).data('type');
                active[type] ? active[type].call(this) : '';
                var form = new FormData(document.getElementById("formFile"));

                //保存表单内容的请求，此处为文件通知对象
                $.ajax({
                    url: "/fproducts/edits",
                    type: "post",
                    data: form,
                    processData: false,
                    contentType: false,
                    success: function (result) {
                        if (result.status === true) {
                            var index = parent.layer.getFrameIndex(window.name);
                            parent.layer.close(index);
                            parent.window.location.reload()
                        } else {
                            layer.msg(result.msg, { icon: 2, time: 1000 });
                        }
                    }
                });
            });
        });
        //重新加载的方法
        layui.use(['table', 'laydate', 'element'], function () {
            var table = layui.table, form = layui.form, $ = layui.jquery;
        });
        layui.use(["form", 'layer', 'layedit'], function () {
            var form = layui.form;
            var $ = layui.$;
            var layedit = layui.layedit;
            layedit.build('demo');
            layedit.set({
                uploadImage: {
                    url: '/fproducts/index' //接口url
                    , type: 'post' //默认post
                   
                }
            });
            //注意：layedit.set 一定要放在 build 前面，否则配置全局接口将无效。
            layedit.build('demo');
            //提交
            form.on('submit(Editform)', function (data) {

                $.post("/fproducts/edits", data.field, function (res) {
                    if (res.isSuccess) {
                        layer.msg(res.msg, function () {
                            parent.window.location.href = "/fproducts/index";
                        });
                    } else {
                        layer.msg(res.msg);
                    }
                });
                return false;
            });
        });
    </script>
</body>
</html>
